package org.lee.fm;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;

import java.io.IOException;
import java.io.StringWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * FreeMarkerDemo
 *
 * @author dralee
 * @date 2021-12-28 18:44:36
 **/
public class FreeMarkerDemo {
    public static void main(String[] args) throws IOException {
        Configuration cfg = new Configuration(new Version("2.3.31"));
        cfg.setClassForTemplateLoading(FreeMarkerDemo.class, "/");
        cfg.setDefaultEncoding("UTF-8");
        Template template = cfg.getTemplate("demo.ftl");
        Map<String, Object> tempData = new HashMap<>();
        tempData.put("msg", "The date is " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        try (StringWriter sw = new StringWriter()) {
            template.process(tempData, sw);
            System.out.println(sw.getBuffer().toString());
            sw.flush();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }
}
